package serialization;

/*
Compile client and server and then start server as follows:

$ java GreetingServer 6066
Waiting for client on port 6066...
Check client program as follows:

$ java GreetingClient localhost 6066
Connecting to localhost on port 6066
Just connected to localhost/127.0.0.1:6066
Server says Thank you for connecting to /127.0.0.1:6066
Goodbye!
*/
import java.net.*;
import java.util.ArrayList;
import java.io.*;

public class StudentClient
{
   public static void main(String [] args)
   {
      String serverName = "localhost";
      int port = Integer.parseInt("6066");
      try
      {
         System.out.println("Client: Connecting to " + serverName + " on port " + port);
         Socket client = new Socket(serverName, port);
         System.out.println("Client: Just connected to " + client.getRemoteSocketAddress());
         
         OutputStream outToServer = client.getOutputStream();
         DataOutputStream out = new DataOutputStream(outToServer);
         out.writeUTF("Hello Server, can you give me a list of students?" + client.getLocalSocketAddress());
         
         
         InputStream inFromServer = client.getInputStream();
         DataInputStream in = new DataInputStream(inFromServer);
         System.out.println("Client gets the message from the server: "+in.readUTF());
         
         ObjectInputStream objectIn = new ObjectInputStream(inFromServer);
         ArrayList<Student> students = (ArrayList<Student>) objectIn.readObject();
         for(int i=0; i<students.size(); i++)
         {
        	 System.out.println(students.get(i).toString());
         }
         
         client.close();
         
      }catch(Exception e)
      {
         e.printStackTrace();
         System.out.println(e);
      }
   }
}